
package com.lemon.ai.socket.packets.spiritgame.model.generator;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generateColumnNames;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generateColumnParameters;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generateColumn_s;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generatePageSets;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generatePkColumns;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generatePkPsSets;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generatePsSets_add;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generatePsSets_update;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser.generateRsGets;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.generateClassName;
import static com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils.generateParameterName;

import java.util.List;

import com.lemon.ai.socket.packets.spiritgame.model.db.MetaData;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Table;
import com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringParser;


/**
 * dao实现类生成器
 * @author dengzongrong
 * @version 1.1
 * @date 2011-10-09
 */
public class DaoImplGenerator extends AbstractGenerator {

	public DaoImplGenerator() {
		super("DaoImplTemp.txt");
	}

	public String generate(Table table) {
		return super.temp.
				replace("PACKAGE_NAME", super.prop.getProperty("package")).
				replace("FINDERS", StringParser.generateFinders(table)).
				replace("CLASS_NAME",generateClassName(table.getName())).
				replace("CLASS_PARAMETER",generateParameterName(table.getName())).
				replace("TABLE_NAME", table.getName()).
				replace("COLUMN_NAMES", generateColumnNames(table.getColumns())).
				replace("COLUMN_?S", generateColumn_s(table.getColumns())).
				replace("PK_PARAMETER", StringParser.generatePkParameters(table)).
				replace("PKCOLUMNS_S", generatePkColumns(table)).
				replace("PK_PS_SETS", generatePkPsSets(table)).
				replace("PS_SETS_ADD", generatePsSets_add(table)).
				replace("PS_SETS_UPDATE", generatePsSets_update(table)).
				replace("PK_COLUMNS", generatePkColumns(table)).
				replace("PAGE_SETS", generatePageSets()).
				replace("RS_GETS", generateRsGets(table)).
				replace("COLUMN_PARAMETERS", generateColumnParameters(table)).
				replace("KEY_COLUMN_NAME", table.getKeys().get(0).getName())
				;
	}

	public String generateFileName(Table table) {
		return super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				+ "\\dao\\impl\\"
				+ generateClassName(table.getName())
				+ "DAOImpl.java";
	}

	public static void main(String[] args) {
		MetaData meta = MetaData.getInstance();
		List<Table> tables = meta.getTables();
		DaoImplGenerator g = new DaoImplGenerator();
		// g.generate("user");
		Table t = new Table();
		t.setName("user");
		// System.out.println(g.generate(t));
		//		System.out.println(g.generate(tables.get(0)));
		System.out.println(g.generate(tables.get(2)));
	}

}
